package service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import bean.Right;
import bean.User;
import bean.model.PageModel;
import dao.RoleDao;
import dao.UserDao;

public class RoleService {
	private RoleDao rd = new RoleDao();
	private UserDao userDao = new UserDao();
	public List<User> getRoleSearchList(String userName, String role, String rigor) {
		List<User> list = new ArrayList();
		Boolean vague = false;
		if(role.equals("0")){
			role = "";
		}else if(role.equals("1")){
			role = "警员";
		}else if(role.equals("2")){
			role = "警官";
		}else{
			role = "管理员";
		}
		if(rigor.equals("true")){
			vague = true;
		}
		String[] arr1={"username","role"};
		String[] arr2={userName, role};
		list = rd.getRoleSearchList(arr1, arr2,vague);
		return list;
	}
	public List <Right> getRightInformation(String username) {
		UserDao ud =new UserDao();
		Set rights = ud.getUser(username).getRights();
		List <Right> list = new ArrayList<Right>();
		list.addAll(rights);
		return list;
	}
	public List <Right> roleUpdate(String userNameUpdate) {
		UserDao ud =new UserDao();
		Set rights = ud.getUser(userNameUpdate).getRights();
		List <Right> list = new ArrayList<Right>();
		list.addAll(rights);
		return list;	
	}
	public boolean roleManagerEdit(String nameUpdate, List<String> checkList) {
		UserDao ud =new UserDao();
		User user = ud.getUser(nameUpdate);
		Set rights = new HashSet(0);
		boolean bl = false;
		for(int i=0;i<checkList.size();i++){
			if((checkList.get(i)).equals("true")){
				Right right = new Right();
				right.setRightId(i);
				rights.add(right);
			}
		}
		user.setRights(rights);
		bl = ud.updateUser(user);
		return bl;
	}
	
    public int getStaffNum(){
		
		int num = userDao.getUserNum();
		
		return num;
		
	}
    
    public List<User> queryStaffByPage(final PageModel page) {
		
    	List<User> staffPage = userDao.queryByPage(page);
    	
		return staffPage;
	}
	
}
